//
//  ImageTextButton.swift
//  HuaYun
//
//  Created by enghou on 2018/7/10.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

import UIKit

@IBDesignable
class ImageTextButton: UIButton {
    
    private var _imgRenderOriginal: Bool = false
    @IBInspectable var imgRenderOriginal: Bool{
        set{
            _imgRenderOriginal = newValue
            guard self.imageView != nil else {
                return
            }
            if newValue {
                self.setImage(self.imageView?.image?.withRenderingMode(.alwaysOriginal), for: .normal)
            }
        }
        get{
            return _imgRenderOriginal
        }
    }
    
    
//    [focusBtn setTitleEdgeInsets:UIEdgeInsetsMake(focusBtn.imageView.frame.size.height ,-focusBtn.imageView.frame.size.width, 0.0,0.0)];//文字距离上边框的距离增加imageView的高度，距离左边框减少imageView的宽度，距离下边框和右边框距离不变
//    [focusBtn setImageEdgeInsets:UIEdgeInsetsMake(-focusBtn.imageView.frame.size.height, 0.0,0.0, -focusBtn.titleLabel.bounds.size.width)];
    
    private var _centering: Bool = true
    @IBInspectable var centering: Bool {
        set{
            _centering = newValue
            
            let imgViewSize = self.imageView?.size ?? CGSize.zero
            let titleSize = self.titleLabel?.size ?? CGSize.zero
            let btnSize = self.size
            let heightSpace: CGFloat = 0.0
            let imageViewEdge = UIEdgeInsets.init(top: heightSpace, left: 0, bottom: btnSize.height - imgViewSize.height - heightSpace, right: -titleSize.width)
            self.imageEdgeInsets = imageViewEdge
            let titleEdge = UIEdgeInsets.init(top: imgViewSize.height + heightSpace, left: -imgViewSize.width, bottom: 0, right: 0)
            self.titleEdgeInsets = titleEdge
        }
        get{
            return _centering
        }
    }
    
    //图片文字的距离,图片在上 文字在下
    private var _txtimggap: CGFloat = 0.0
    @IBInspectable var txtimggap: CGFloat{
        set{
            
            _txtimggap = newValue
            guard self.imageView != nil && self.titleLabel != nil else {
                return
            }
            
            let imageW = self.imageView!.width
            
            let imageH = self.imageView!.height
            
            let titleW = self.titleLabel!.width
            
            let titleH = self.titleLabel!.height
            
            self.titleEdgeInsets = UIEdgeInsets.init(top: 0.0, left: -imageW, bottom: -imageH - newValue, right: 0.0)
            
            self.imageEdgeInsets = UIEdgeInsets.init(top: -titleH - newValue, left: 0.0, bottom: 0.0, right: -titleW)
            
        }
        get{
            return _txtimggap
        }
    }
    
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
